using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Magiq.Behaviors;

namespace Magiq.Insert {
    public class InsertDefinition<TSource, TDestination, TNew> : BehaviorBasedExecution<IInsertBehavior<TSource, TDestination, TNew>>, IInsertBehavior<TSource, TDestination, TNew>, IInsertDefinition<TSource, TDestination, TNew>
        where TNew : class
        where TDestination : class
    {
        public InsertDefinition(IEnumerable<TSource> source, Expression<Func<TSource, TNew>> newValueExpression, IEnumerable<TDestination> destination,IMagiqBehavior behavior) : base(behavior)
        {
            Destination = destination;
            Source = source;
            NewExpression = newValueExpression;
        }

        public Expression<Func<TSource, TNew>> NewExpression { get; private set; }

        public IEnumerable<TSource> Source { get; private set; }
        
        public IEnumerable<TDestination> Destination { get; private set; }

        public int Execute() {
            var strategy = Call(x => x.GetInsertStrategy(this));
            return Call(x => x.Execute(strategy));
        }

        IInsertStrategy IInsertBehavior<TSource, TDestination, TNew>.GetInsertStrategy(IInsertDefinition<TSource, TDestination, TNew> insertDefinition)
        {
            return Magiq.ForInsert(insertDefinition);
        }

        public int Execute(IInsertStrategy strategy)
        {
            return strategy.Execute();
        }
    }
}